﻿using BaseUtils.Utils;
using Service.src.Model.Folw;
using Service.src.NW.Enum;
using Service.src.NW.Model.Enum;
using Service.src.NW.Model.Folw;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Service.src.NW.Processor.Core
{
    public class FileIOProcessor : BaseProcessor
    {
        internal static void Run(NodeProcessor nodeProcessor, NodeInputParam InputParam)
        {
            switch (nodeProcessor.nodeType)
            {
                case NodeTypeEnum.FILE_IO_READ:
                    NodeDataSource NDS = GetNodeInputParam(nodeProcessor, InputParam.NodeFile);
                    string FilePath = NDS.Value;
                    if (!File.Exists(FilePath))
                    {
                        SendNodeLoggerError(nodeProcessor, $"文件路径错误，文件不存在,地址：{FilePath}");
                        Complete(nodeProcessor, NodeStatusEnum.ERROR);
                        return;
                    }
                    List<string> content = FileHelper.ReadContentWithStreamReader(FilePath,
                        InputParam.NodeFile.Read.ReadLine,
                        InputParam.NodeFile.EncodingFormat);
                    SetNodeOutParamValue(nodeProcessor, NodeOutParamNameEnum.DEVICE_READ_RESULT, content);
                    Complete(nodeProcessor, NodeStatusEnum.SUCCESS);
                    break;
                case NodeTypeEnum.FILE_IO_WRITE:
                    break;
                case NodeTypeEnum.FILE_IO_COPY:
                    NodeStatusEnum Status = NodeStatusEnum.SUCCESS;
                    string SourceFile = GetNodeInputParamValue(nodeProcessor, InputParam.NodeFile.Read);
                    string DestinationFile = GetNodeInputParamValue(nodeProcessor, InputParam.NodeFile.Write);

                    if (string.IsNullOrEmpty(SourceFile) || string.IsNullOrEmpty(DestinationFile))
                    {
                        Status = NodeStatusEnum.ERROR;
                        SendNodeLoggerError(nodeProcessor, $"文件地址或者目标文件夹等参数没有维护，请检查，文件地址：{SourceFile}, 目标地址：{DestinationFile}");
                    }
                    if (Status == NodeStatusEnum.SUCCESS && !File.Exists(SourceFile))
                    {
                        Status = NodeStatusEnum.ERROR;
                        SendNodeLoggerError(nodeProcessor, $"找不到文件，请检查文件地址后重试，文件地址：{SourceFile}");
                    }
                    if (Status == NodeStatusEnum.SUCCESS)
                    {
                        string FileName = GetNodeInputParamValue(nodeProcessor, InputParam.NodeFile.Write.FileName);
                        if (string.IsNullOrEmpty(FileName))
                        {
                            FileName = Path.GetFileName(SourceFile); ;
                        }
                        FileHelper.CreateDirectory(DestinationFile);
                        FileHelper.CopeFile(SourceFile, DestinationFile + "\\" + FileName);
                    }
                    Complete(nodeProcessor, Status);
                    break;
            }
        }

        /// <summary>
        /// "LOSS_L73B-2-DJY: TOM系统反馈操作：PLC地址M1732.3，值：True: 写入成功2024/11/28 00:00:11.199 "
        /// </summary>
        /// <param name="Content"></param>
        public static void Transformation(List<string> Content)
        {
            List<string> s = new List<string>();
            StringBuilder stringBuilder = new StringBuilder();
            foreach (string item in Content)
            {
                if (string.IsNullOrEmpty(item))
                {
                    continue;
                }
                string[] strings = item.Split(new char[] { ':' });
                if (strings[0].StartsWith("LOSS_") && strings[1].EndsWith("True"))
                {
                    s.Add(strings[0]);
                    stringBuilder.AppendLine(strings[0].Replace("LOSS_", ""));
                }
            }
            string g = stringBuilder.ToString();
        }
    }
}
